$(function(){
	var exam_id = $('#exam_id').val();
	if(exam_id!=null&&exam_id!=""&&exam_id!="null"){
		$.ajax({
			url:'stuAnswer/getTask',
			type:'post',
			dataType:'json',
			async:false,
			data:{exam_id:exam_id},
			success:function(data){
				//{'num':0,'stuAnswer':{}}
				//console.log(JSON.stringify(data));
				if(data.num==-1){
					alert("出错了...");
				}else if(data.num==0){
					alert("阅卷完毕!");
					window.location.href="tch_exam";
				}else if(data.num>0){
					loadStuAnswer(data);
				}else{
					alert("出错了...");
				}
			},
			error:function(){
				Modal.alert({msg:'出错！'});
			}
		});
	}else{
		alert("空数据!");
		//window.location.href="tch_index";
	}
});
function cbrequired(obj){
	//复选框非空
	if($('input[name="'+$(obj).attr("name")+'"]:checked').length<=0){
		$($('input[name="'+$(obj).attr("name")+'"]')[0]).prop('required','required');
	}else{
		$('input[name="'+$(obj).attr("name")+'"]').removeProp('required');
	}
}

function initBlanks(){
	var blanks = $('.exam-subtopics-title').find('input[type="text"]');
	for(var i=0;i<blanks.length;i++){
		$(blanks[i]).val($(blanks[i]).attr('content'));
		$(blanks[i]).removeAttr('content');
		$(blanks[i]).attr('disabled',true);
	}
}
function updateGradeScore(){
	var total_score = parseFloat(0);
	var inputs = $('input[name="input_score"]');
	var subtopics_score = "";
	for(var i=0;i<inputs.length;i++){
		var score = "";
		var max = parseFloat($(inputs[i]).attr('max'));
		var input_v = $(inputs[i]).val();
		if(!isNaN(input_v)&&input_v>=0&&input_v<max&&input_v!=""){
			score = $(inputs[i]).val();
			total_score += parseFloat($(inputs[i]).val());
		}
		subtopics_score += '<span class="grade-score-item">'+(i+1)+'、<font color="blue">'+score+'</font>&nbsp;&nbsp;</span>';
	}
	$('.grade-score-container').html(subtopics_score);
	$('#grade_score').text(total_score);
}
//表单提交
$('#mark_commit_form').submit(function(event){
	var stuAnswer_id = $('#stuAnswer_id').val();
	var stuAnswer = {'id':stuAnswer_id};
	var stuOptions = [];
	
	var subtopics = $('.paper-subtopics-item');
	for(var i=0;i<subtopics.length;i++){
		var stuOptions_id = $(subtopics[i]).find('.stuoptions-id').val();
		var score = $(subtopics[i]).find('input[name="input_score"]').val();
		stuOptions.push({'id':stuOptions_id,'score':score});
	}
	
	$(stuAnswer).attr('stuOptions',stuOptions);
	var result = JSON.stringify(stuAnswer);
	console.log(result)
	$.ajax({
		url:'stuAnswer/markOne',
		type:'post',
		dataType:'json',
		data:{stuAnswer:result},
		success:function(data){
			//{'num':0,'stuAnswer':{}}
			//console.log(JSON.stringify(data));
			if(data.num==-1){
				alert("出错了...");
			}else if(data.num==0){
				alert("阅卷完毕!");
				window.location.href="tch_exam";
			}else if(data.num>0){
				loadStuAnswer(data);
			}else{
				alert("出错了...");
			}
		},
		error:function(){
			alert('出错了...');
		}
	});
	return false;
});
/*生成唯一num*/
function newGuid()  
{  
    var guid = "";  
    for (var i = 1; i <= 32; i++){
      var n = Math.floor(Math.random()*16.0).toString(16);  
      guid +=   n;  
      if((i==8)||(i==12)||(i==16)||(i==20))  
        guid += "-";
    }  
    return guid;      
}
//加载答卷
function loadStuAnswer(data){
	//加载答卷信息
	
	var stuAnswer = data.stuAnswer;
	
	var bodyHtml = '<input type="hidden" id="stuAnswer_id" value="'+stuAnswer.id+'">';
	
	var total_score = parseFloat(0);
	var _index = 0;
	for(var i=0;i<stuAnswer.stuOptions.length;i++){
		var stuOptions = stuAnswer.stuOptions[i];
		var subtopics = stuAnswer.stuOptions[i].subtopics;
		if(subtopics.blank!=null){
			total_score += parseInt(subtopics.score);
			_index++;
			//填空题
			var blank = subtopics.blank;
				bodyHtml += '<div class="paper-subtopics-item">'+
							'<input type="hidden" class="stuoptions-id" value="'+stuOptions.id+'">'+
							'<input type="hidden" class="subtopics-index-value" value='+_index+'>'+
							'<div class="paper-subtopics-index"><span>'+_index+'</span>、</div>'+
							'<div onkeyup="divInputChange(this)" class="exam-subtopics-title">';
				var contentDOM = $('<body></body>');
				$(contentDOM).html(blank.title);
				
				var blank_answers = "";
				var blank_list = $(contentDOM).find('input');
				for(var x=0;x<blank_list.length;x++){
					blank_answers += blank.blanks[x+1].content + '&nbsp;&nbsp;&nbsp;&nbsp;';
					$(blank_list[x]).attr('onchange','');
					if(stuOptions.options[x+1]!=null){
						$(blank_list[x]).attr('content',stuOptions.options[x+1].content);
					}
				}
				bodyHtml += $(contentDOM).html();
				
				console.log('blank的题干:'+$(contentDOM).html())
				
				bodyHtml +=	'</div>'+
						'<font color="blue"><i>(填空题,'+subtopics.score+'分)</i></font>';
				bodyHtml += '<div>参考答案：</div><font color="blue">'+blank_answers+'</font>';
				bodyHtml += '<div style="margin-top:8px;">得分:<input onkeyup="updateGradeScore()" name="input_score" required type="number" min="0.0" max="'+subtopics.score+'" placeholder="评分"></div>'+
							'</div>';
		}else if(subtopics.explanation!=null){
			total_score += parseInt(subtopics.score);
			_index++;
			//解答题
			var explanation = subtopics.explanation;
			bodyHtml += '<div class="paper-subtopics-item">'+
						'<input type="hidden" class="stuoptions-id" value="'+stuOptions.id+'">'+
						'<input type="hidden" class="subtopics-index-value" value='+_index+'>'+
						'<div class="paper-subtopics-index"><span>'+_index+'</span>、</div>'+
						'<div class="exam-subtopics-title">'+
							explanation.title+
						'</div>'+
						'<font color="blue"><i>(解答题,'+subtopics.score+'分)</i></font>'+
						'<div class="paper-subtopics-body">'+
						'</div>';
			bodyHtml += '<div class="question-answer">考生作答：'+
				'<textarea disabled class="form-control answer-textarea question-options-content">';
				if(stuOptions!=null){
					console.log('考生作答:'+stuOptions.options[1].content);
					bodyHtml += stuOptions.options[1].content;
				}
			bodyHtml += '</textarea>';
			bodyHtml += '<div>参考答案：</div><font color="blue">'+explanation.reference.content+'</font>';
			bodyHtml += '<div style="margin-top:8px;">得分:<input onkeyup="updateGradeScore()" name="input_score" required type="number" min="0.0" max="'+subtopics.score+'" placeholder="评分"></div>';
			bodyHtml += '</div>'+'</div>';
		}else{
			
		}
	}
	console.log(total_score);
	$('#leave_num').text(data.num);
	$('#total_score').text(total_score);
	$('.exam-body').html(bodyHtml);
	initBlanks();
	updateGradeScore();
}